﻿using DLLPC.Utilities;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Logical;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VisionCore
{
    public class Status_ExcInsp : ProcedureHelper_Exc
    {
        string subStatusCode = "";
        string tempStatusCode = "";
        bool condition = false;
        bool start = false;
        int Row = 1;
        int Col = 1;
        SystemTimeCounter scStatus = new SystemTimeCounter();
        public void Main()
        {
            condition = /*(ExcST_Presence || ExcST_Empty) &&*/ Kernel.motion.ExcST_R.IsHomeAlready && Kernel.motion.ExcST_R.IsHomeAlready && Kernel.motion.ExcCCD.IsHomeAlready
                     && Kernel.motion.ExcTR_X.IsHomeAlready && Kernel.motion.ExcTR_R.IsHomeAlready && Kernel.motion.ExcTR_Z.IsHomeAlready;
            start = condition && VisionAutoMode && AutoMode && (ExcSTStatusCode == ExcSTStatus.PreAlignReq|| ExcSTStatusCode == ExcSTStatus.ExchangeResultInsp);

            switch (ExcInspStatusCode)
            {
                case ExcSTInspStatus.Init:
                    subStatusCode = "00: 准备";
                    {
                        RTCHelper.DIO.L_ExcSTRowCount = 9;
                        RTCHelper.DIO.L_ExcSTColCount = 6;
                        RTCHelper.DIO.L_ExcSTCurrentRow = 1;
                        RTCHelper.DIO.L_ExcSTCurrentCol = 1;
                        if (ParameterHelper.ExcST.Wafer_ID!=null)
                        {
                            RTCHelper.DIO.L_ExcSTWaferID = ParameterHelper.ExcST.Wafer_ID;
                        }

                        if (start)
                        {
                           
                                RTCHelper.DIO.L_ExcSTFuncIndex = 2;
                                scStatus.Start();
                                ExcInspStatusCode = ExcSTInspStatus.CheckStatus;
                           
                        }

                    }
                    break;
                case ExcSTInspStatus.CheckStatus:
                    subStatusCode = "05: 状态判断";
                    {
                        if (scStatus.Duration > 200 && VisionAutoMode && AutoMode)//20230412liang
                        {
                            
                        }
                        
                    }
                    break;
                case ExcSTInspStatus.ToAlignPos:
                    subStatusCode = "06: YRX移动到检测位";
                    {  if(ExcST_ExcInsp && ExcCCD_ExcInsp)
                        {
                            ExcInspStatusCode = ExcSTInspStatus.AtAlignPos;
                            scStatus.Start();
                        }

                    }
                    break;
                case ExcSTInspStatus.AtAlignPos:
                    subStatusCode = "07: YRX在检测位";
                    {
                        if(scStatus.Duration > 100)
                        {
                            ProcedureHelper_Exc.ExcSTInsped = false;
                            ExcInspStatusCode = ExcSTInspStatus.Trigger;
                        }

                    }
                    break;
                case ExcSTInspStatus.Trigger:
                    subStatusCode = "08: 检查触发";
                    {
                        RTCHelper.IOEvent.HSG_ExcSTInspTrigger.Trig();
                        scStatus.Start();
                        ExcInspStatusCode = ExcSTInspStatus.TriggerComplete;
                    }
                    break;
                case ExcSTInspStatus.TriggerComplete:
                    subStatusCode = "09: 检测触发完成";
                    {
                        if (scStatus.Duration>5000|| ExcSTInsped)
                        {
                            if (ProcedureHelper_Exc.ExcSTInsped)
                            {
                                if (ExcSTStatusCode == ExcSTStatus.ExchangeResultInsp && ProcedureHelper_Exc.ExcSTInspRet == 2)
                                {
                                    //ParameterHelper.ExcST.ExcRetInspFlag = true;
                                    //ParameterHelper.ExcST.ExcRet = true;
                                    SaveExcST();
                                    ExcInspStatusCode = ExcSTInspStatus.Init;
                                }
                                else
                                {
                                    // S形扫描
                                    if (RTCHelper.DIO.L_ExcSTCurrentRow == RTCHelper.DIO.L_ExcSTRowCount && ((RTCHelper.DIO.L_ExcSTCurrentRow % 2 == 1 && RTCHelper.DIO.L_ExcSTCurrentCol == RTCHelper.DIO.L_ExcSTColCount) || (RTCHelper.DIO.L_ExcSTCurrentRow % 2 == 0 && RTCHelper.DIO.L_ExcSTCurrentCol == 0)))
                                    {
                                        if (ExcSTStatusCode == ExcSTStatus.PreAlignReq)                                       
                                            SaveExcST();
                                            ExcInspStatusCode = ExcSTInspStatus.Init;
                                    }
                                    else if (ExcSTStatusCode == ExcSTStatus.ExchangeResultInsp && ProcedureHelper_Exc.ExcSTInspRet == 1)
                                    {
                                        //ParameterHelper.ExcST.ExcRetInspFlag = true;
                                        //ParameterHelper.ExcST.ExcRet = false;
                                        SaveExcST();
                                        ExcInspStatusCode = ExcSTInspStatus.Init;
                                    }
                                    
                                    else
                                    {
                                        if (RTCHelper.DIO.L_ExcSTCurrentRow <= RTCHelper.DIO.L_ExcSTRowCount)
                                        {
                                            if ((RTCHelper.DIO.L_ExcSTCurrentCol < RTCHelper.DIO.L_ExcSTColCount) && RTCHelper.DIO.L_ExcSTCurrentRow % 2 == 1)
                                            {
                                                RTCHelper.DIO.L_ExcSTCurrentCol++;
                                            }
                                            else if ((RTCHelper.DIO.L_ExcSTCurrentCol > 1) && (RTCHelper.DIO.L_ExcSTCurrentCol <= RTCHelper.DIO.L_ExcSTColCount) && RTCHelper.DIO.L_ExcSTCurrentRow % 2 == 0)
                                            {
                                                RTCHelper.DIO.L_ExcSTCurrentCol--;
                                            }
                                            else if ((RTCHelper.DIO.L_ExcSTCurrentCol == RTCHelper.DIO.L_ExcSTColCount && RTCHelper.DIO.L_ExcSTCurrentRow % 2 == 1) || (RTCHelper.DIO.L_ExcSTCurrentCol == 1 && RTCHelper.DIO.L_ExcSTCurrentRow % 2 == 0))
                                            {
                                                RTCHelper.DIO.L_ExcSTCurrentRow++;
                                            }
                                            ExcInspStatusCode = ExcSTInspStatus.CheckStatus;
                                        }
                                    }


                                    ////Z形扫描
                                    //if (RTCHelper.DIO.L_ExcSTCurrentRow == RTCHelper.DIO.L_ExcSTRowCount && RTCHelper.DIO.L_ExcSTCurrentCol == RTCHelper.DIO.L_ExcSTColCount)
                                    //{
                                    //    if (ExcSTStatusCode == ExcSTStatus.PreAlignReq)
                                    //    {
                                    //        ParameterHelper.ExcST.ExcPreAlignFlag = true;
                                    //        SaveExcST();
                                    //        ExcInspStatusCode = ExcSTInspStatus.Init;
                                    //    }
                                    //    else if (ExcSTStatusCode == ExcSTStatus.ExchangeResultInsp && ProcedureHelper_Exc.ExcSTInspRet == 1)
                                    //    {
                                    //        ParameterHelper.ExcST.ExcRetInspFlag = true;
                                    //        ParameterHelper.ExcST.ExcRet = false;
                                    //        SaveExcST();
                                    //        ExcInspStatusCode = ExcSTInspStatus.Init;
                                    //    }
                                    //}
                                    //else
                                    //{
                                    //    if (RTCHelper.DIO.L_ExcSTCurrentCol < RTCHelper.DIO.L_ExcSTColCount)
                                    //    {
                                    //        RTCHelper.DIO.L_ExcSTCurrentCol++;
                                    //    }
                                    //    else
                                    //    {
                                    //        RTCHelper.DIO.L_ExcSTCurrentRow++;
                                    //        RTCHelper.DIO.L_ExcSTCurrentCol = 1;
                                    //    }
                                    //    ExcInspStatusCode = ExcSTInspStatus.CheckStatus;
                                    //}
                                }
                            }
                            else
                            {
                                ProcedureHelper_Exc.ExcSTInsped = false;
                                ExcInspStatusCode = ExcSTInspStatus.Trigger;
                            }
                        }
                        
                    }
                    break;
                default:
                    break;
            }
            LogStatusCode(subStatusCode);
        }

        private void LogStatusCode(string msg)
        {
            if (msg != tempStatusCode)
            {
                tempStatusCode = msg;
                LogMsg("倒膜Maping检查- " + tempStatusCode);
            }
        }
    }
}
